package com.hucheng.job.jobhandler;

import com.alibaba.fastjson.JSONObject;
import com.hucheng.common.utils.HttpClientUtils;
import com.hucheng.common.utils.RSAUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 跨平台Http任务
 *
 * @author xuxueli 2018-09-16 03:48:34
 */
@Slf4j
@JobHandler(value = "httpJobHandler")
@Component
public class HttpJobHandler extends IJobHandler {

    private static final String URL = "http://localhost:9090/rest/";
    private static final String FORMAT = "json";
    public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2HL0Wr+F2JkD2TZOoB0kJW9onz0HL52zjhIZxXKO71b72XEtMRujAxu0jZneHfKdST55t3wZ9xLgx21kTqMB9q/RtCq4x5HT/0cTCCae/i4wwi484hsG/SYynLAdZbV4GEiqQ0HZU/aPPenLe7NY0+5N2Bg8KqseKFdWPzaccxQIDAQAB";
    public static final String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALYcvRav4XYmQPZNk6gHSQlb2ifPQcvnbOOEhnFco7vVvvZcS0xG6MDG7SNmd4d8p1JPnm3fBn3EuDHbWROowH2r9G0KrjHkdP/RxMIJp7+LjDCLjziGwb9JjKcsB1ltXgYSKpDQdlT9o896ct7s1jT7k3YGDwqqx4oV1Y/NpxzFAgMBAAECgYArt+h46s4wY+ySdHsvClixQ3iyt+GuxqbwU59G1ZuE+0I1uGBnNlqn9Nl29yFYyEcOzohVDgZr6O7jISpYOtnCqauns0vUQBWGNMkG0CwrdVPAX2yK/eUkXJd7cIms2pgUXj0P0Tag0R1ajYwBeq9KyY0WJHiPtMNB3B0hZMMMXQJBAOk+88mAqOVJsTy/3rrOWHwpKSVRZmgQFO35eiEY3TwWFeiPp3t8oF1s9hLms4hXns4nrXHnDvCBu5PR6Ddj6LMCQQDH4MhNcCApIxn0ds+66KOEXAVQUzozpkmYYlg1857c3YoJhuWFGW8/itT/G5YbNhKwklXz98Bpoa6PkZERpHCnAkEAohdFg/sNZIpZyUXtmxRbGa3hEjYHXLoQUXDWFroYxPD0OLHcSkzJhSN7vpbknROHIO0h884G68y8dnWhUxzzUQJANz9/vj/m89f6/aK1qqFRSW2yHlDS/qHRJ+jPHyKW38/wrs91s8OjB3pVg6pZZG4e3mWkkooBynDhcle3NJwycwJANiw3Q9MWutTxIu+FODn0WKHilb2GkCXnGqEnVYfrqEuCZ4fh0TUvvzCjYs+5hvkfP1vIzPxQaRxNTIUdoX/HkQ==";


    /**
     * @param param 是json参数--->url,action,accessId,format,reqObject
     * @return
     * @throws Exception
     */
    @Override
    public ReturnT<String> execute(String param) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(param);

        //获取默认的accessId
        String accessId = getAccessId();
        //header头部信息
        Map<String, Object> maps = new LinkedHashMap<>();
        maps.put("Content-Type", "application/json");
        maps.put("action", jsonObject.getString("action"));
        maps.put("accessId", accessId);
        maps.put("format", FORMAT);
        maps.put("userId", "0");
        maps.put("reqObject",jsonObject.get("reqObject").toString().replaceAll(":","=").replaceAll("\"","").replaceAll(" ",""));

        String p = RSAUtils.getParams(maps);

        String encryptByPublicKey = Base64.encodeBase64String((RSAUtils.encryptByPublicKey(p.getBytes("utf-8"), publicKey)));

        maps.put("sign", encryptByPublicKey);

        log.info(">>>>>>>>>>> 执行定时器 " + jsonObject.get("action") + " 请求参数 " + jsonObject);
        String result = HttpClientUtils.doPostJson(URL, maps, JSONObject.toJSONString(jsonObject));
        log.info(">>>>>>>>>>> 执行定时器 " + jsonObject.get("action") + " 返回结果 " + result);
        return SUCCESS;
    }

    /**
     * 先获取默认的accessId
     *
     * @return
     */
    public String getAccessId() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("reqObject", new JSONObject());

        //header头部信息
        Map<String, Object> maps = new LinkedHashMap<>();
        maps.put("Content-Type", "application/json");
        maps.put("action", "user_defaultAccessId");
        maps.put("accessId", "");
        maps.put("format", "json");
        maps.put("userId", "0");
        maps.put("reqObject",new JSONObject().toString().replaceAll(":","=").replaceAll("\"","").replaceAll(" ",""));

        String p = RSAUtils.getParams(maps);

        String encryptByPublicKey = Base64.encodeBase64String((RSAUtils.encryptByPublicKey(p.getBytes("utf-8"), publicKey)));

        maps.put("sign", encryptByPublicKey);

        String result = HttpClientUtils.doPostJson(URL, maps, JSONObject.toJSONString(jsonObject));
        JSONObject jb = JSONObject.parseObject(result);
        jb = jb.getJSONObject("data");
        return jb.getString("accessId");
    }



}
